home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1997 August / Walnut Creek CDROM.7z / LISTINGS / V_12_05 / ALLISON.ZIP / STR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-04  |  556 b   |  40 lines

  1.  
  2. LISTING 5 - Implementation for Listing 4
  3. // str.cpp
  4.  
  5. #include <iostream.h>
  6. #include <string.h>
  7. #include "str.h"
  8.  
  9. String::String(const char *buf)
  10. {
  11.     if (!buf)
  12.         rep = new Srep("");
  13.     else
  14.         rep = new Srep(buf);
  15. }
  16.  
  17. String::String(const String& s)
  18. {
  19.     rep = s.rep;
  20.     rep->count++;
  21. }
  22.  
  23. String::~String()
  24. {
  25.     if (--rep->count <= 0)
  26.         delete rep;
  27. }
  28.  
  29. String::Srep::Srep(const char* s)
  30. {
  31.     strcpy(rep = new char[strlen(s)+1], s);
  32.     count = 1;
  33. }
  34.  
  35. String::Srep::~Srep()
  36. {
  37.     delete [] rep;
  38. }
  39.  
  40.